import { reactive, ref } from 'vue';
import { defineStore } from 'pinia';
import { SetupStoreId } from '@/enum';
import { fetchGetSystemInfo } from '@/service/api';

export const usePlatformStore = defineStore(SetupStoreId.PlatForm, () => {
  const systemName = import.meta.env.VITE_APP_TITLE;
  const platformInfo = reactive<CommonApi.Platform.PlatformInfo>(initPlatformInfo());
  const isInitPlatformInfo = ref(false);

  /** 初始化平台信息 */
  function initPlatformInfo() {
    // 初始化平台信息
    return {
      systemName
    };
  }

  /** 获取平台信息 */
  async function getPlatformInfo() {
    if (!isInitPlatformInfo.value) {
      const { data, error } = await fetchGetSystemInfo();
      if (error) return;
      Object.assign(platformInfo, data);
      isInitPlatformInfo.value = true;
    }
  }

  return {
    platformInfo,
    isInitPlatformInfo,
    getPlatformInfo
  };
});
